/**
 * 用户选择器
 * @class UserDialog
 * @extends Ext.Window
 */
UserDialog=Ext.extend(Ext.Window,{
	constructor:function(conf){
		Ext.applyIf(this,conf);
		
		this.scope=this.scope || this;
		//默认为多单选择用户
		this.single = this.single!=null ? this.single : true;
		
		this.initUI();
		UserDialog.superclass.constructor.call(this,{
			title: this.title || '用户选择器',
			height:450,
			width: 730,
			maximizable : true,
			modal:true,
			layout:'border',
			items:[
				this.westPanel,
				this.searchPanel,
				this.gridPanel
			],
			buttonAlign:'center',
			buttons:[{
				text:'确定',
				iconCls:'btn-ok',
				scope:this,
				handler:this.confirm
			},{
				text:'取消',
				iconCls:'btn-cancel',
				scope:this,
				handler:this.close
//			},{
//				text : '发起人',
//				id:'startUser',
//				iconCls : 'menu-subuser',
//				scope : this,
//				handler : function(){
//					this.callback.call(this, '__start', '[发起人]');
//					this.close();
//				},
//				disabled:true
			}]
		});
//		if(this.isForFlow){
//			Ext.getCmp('startUser').disabled = false;
//		}
		if(!this.single){
			this.add(this.southPanel);
			this.doLayout();
		}
	},
	//按组织架构查找用户
	orgTreeClick:function(){
		var orgId=this.orgTreePanel.selectedNode.id;
		var store = this.gridPanel.getStore();
		var baseParams=null;
		if(orgId!=0){	//若orgId==0,则代表为所有用户
			baseParams = {orgId: orgId};
		}else{
			baseParams={};
		}
		baseParams.start = 0;
		store.proxy.extraParams = baseParams;
		store.reload();
	},
	//按职位查找用户
	posTreeClick:function(){
	 	var posId=this.posTreePanel.selectedNode.id;
		var store = this.gridPanel.getStore();
		var baseParams=null;
		if(posId!=0){	//若orgId==0,则代表为所有用户
			baseParams = {'Q_positions.posId_L_EQ':posId};
		}else{
			baseParams={};
		}
		baseParams.start = 0;
		store.proxy.extraParams = baseParams;
		store.reload();
	},
	//按角色查找用户
	roleTreeClick:function(){
		var roleId=this.rolePanel.selectedNode.id;
		var store = this.gridPanel.getStore();
		var baseParams=null;
		if(roleId!=0){	//若orgId==0,则代表为所有用户
			baseParams = {'Q_roles.roleId_L_EQ':roleId};
		}else{
			baseParams={};
		}
		baseParams.start = 0;
		store.proxy.extraParams = baseParams;
		store.reload();
	},
	//查找所有在线用户
	onlineClick:function(){
		
	},
	demensionSel:function(combo,record,index){
		var demId=combo.getValue();
		this.orgTreePanel.loader=new Ext.tree.TreeLoader(
	    {
	        baseParams:{demId:demId},
	        dataUrl: __ctxPath+'/system/treeDepartment.do',
	        requestMethod:'GET'
	    });
	    this.orgTreePanel.selectedNode=null;
	    this.orgTreePanel.root.reload();
	},
	/**
	 * 初始化UI
	 */
	initUI:function(){
//		this.demStore=new Ext.data.ArrayStore({
//			autoLoad:true,
//			url:__ctxPath+'/system/comboDemension.do',
//			fields:['id','name']
//		});
//		//维度选择下拉
//		this.demensionCombo=new Ext.form.ComboBox({
//			displayField:'name',
//			valueField:'id',
//			editable : false,
//			emptyText:'所有维度',
//			mode : 'local',
//			triggerAction : 'all',
//			store:this.demStore,
//			listeners:{
//			         scope: this,
//			         'select':this.demensionSel
//			}
//		});
		
		//组织树Panel
		this.orgTreePanel=new Ext.ux.TreePanelEditor({
			border:false,
			url : __ctxPath+'/system/treeDepartment.do',
			region:'center',
			scope:this,
			autoScroll:true,
			onclick:this.orgTreeClick
		});
		//岗位树Panel
//		this.posTreePanel=new Ext.ux.TreePanelEditor({
//			title:'按岗位查找',
//			border:false,
//			url : __ctxPath+'/system/treePosition.do',
//			scope : this,
//			autoScroll:true,
//			onclick:this.posTreeClick
//		});
		
		this.orgPanel=new Ext.Panel({
			border:false,
			title:'按组织架构查找',
			layout:'fit',
			items:[{
					xtype:'panel',
					layout:'border',
					border:false,
					items:[
//						{
//							xtype:'panel',
//							region:'north',
//							border:false,
//							autoHeight:true,
//							layout:'fit',
//							items:this.demensionCombo
//						},
						this.orgTreePanel
					]
				}
			]
		});
		//__ctxPath + '/system/treeAppRole.do'
		
		this.rolePanel=new Ext.ux.TreePanelEditor({
			border:false,
			title:'按角色查找',
			url : __ctxPath + '/system/treeAppRole.do',
			scope:this,
			autoScroll:true,
			onclick:this.roleTreeClick
		});
		
//		this.onlinePanel = new Ext.Panel({
//			collapsible:false,
//			border:false,
//			autoScroll : true,
//			iconCls : 'online-user',
//			title : '在线人员  ',
//			listeners : {
//				scope:this,
//				'expand' : this.onlineClick
//			}
//		});
		
		//按逻辑代码创建
		
	 	this.westPanel=new Ext.Panel({
	 		split:true,
	 		collapsible : true,
			split : true,
			region:'west',
			header:false,
	 		//title:'导航',
	 		width:210,
	 		layout : 'accordion',
	 		collapsible : true,
	 		items:[this.orgPanel,this.rolePanel/*,this.posTreePanel,this.onlinePanel*/]
	 	});
		//搜索用户列
	 	this.searchPanel=new KE.SearchPanel({
		 		region:'north',
				items : [{
					text:'用户姓名：'
				},{
					xtype:'textfield',
					width:200,
					name:'Q_fullname_S_LK'
				},{
					xtype:'button',
					iconCls:'btn-search',
					text:'查询',
					scope:this,
					handler:function(){
						$search({
							searchPanel : this.searchPanel,
							gridPanel : this.gridPanel
						});
					}
				}, {
					xtype : 'button',
					text : '清空',
					scope : this,
					iconCls : 'reset',
					handler : function() {
						this.searchPanel.getForm().reset();
					}
				}]
	 	});
	 	//可选择的用户列表
	 	this.gridPanel=new KE.GridPanel({
	 		singleSelect:this.single,
	 		title:'可选用户',
	 		region:'center',
	 		isShowTbar:false,
	 		url:__ctxPath + '/system/dialogAppUser.do?curDep=',//默认查找当前用户所在单位的用户
	 		fields :[{name : 'userId',type : 'int'}, 'fullname', 'title', 'mobile'],
	 		columns:[
	 			{
					header : "用户名",
					dataIndex : 'fullname',
					renderer : function(value,meta,record){
						var title = record.data.title;
						if(title == 1)
							return '<img src="'+__ctxPath+'/images/flag/man.png"/>&nbsp;' + value;
						else
							return '<img src="'+__ctxPath+'/images/flag/women.png"/>&nbsp;' + value;
					},
					width : 60
				}
	 		]
	 	});

	 	if(!this.single){
	 		
	 		this.gridPanel.addListener('rowclick',this.gridPanelRowClick,this);
	 		
	 		//已选的用户列表
		 	this.selGridPanel = new KE.GridPanel({
		 		title:'已选用户(双击行移除)',
		 		split:true,
				isShowTbar:false,
				width : 180,
				height: 430,
				showPaging:false,
				autoScroll : true,
				selModel: 'rowmodel',
				store : new Ext.data.ArrayStore({
	    			fields : ['userId', 'fullname','title']
				}),
				columns : [{
					header : "用户名",
					dataIndex : 'fullname',
					renderer : function(value,meta,record){
						var title = record.data.title;
						if(title == 1)
							return '<img src="'+__ctxPath+'/images/flag/man.png"/>&nbsp;' + value;
						else if(title == 0)
							return '<img src="'+__ctxPath+'/images/flag/women.png"/>&nbsp;' + value;
						else
							return value;
					}
				}],
				listeners:{
		 			scope:this,
		 			'rowdblclick':this.selGridPanelRowDbClick
		 		}
			}); // end of this selectedUserGrid
		 	
		 	this.southPanel = new Ext.Panel({
		 		width:220,
				region:'east',
				layout:'column',
				border:false,
				items:[new Ext.Panel({
					frame:true,
					width:35,
					height: 430,
					layout: {
                                type:'vbox',
                                pack:'center',
                                align:'stretch'
                            },
                    defaults:{margin:'0 0 5 0'},
                    defaultType : 'button',
                    items : [{
                    	iconCls : 'add-all',
                    	text : '',
                    	scope : this,
                    	handler : this.gridPanelRowClick
                    },{
                    	iconCls : 'rem-all',
                    	text : '',
                    	scope : this,
                    	handler : this.selGridPanelRowDbClick
                    }]
				}),{
					 autoScroll : true,
					 items:[this.selGridPanel]
				}]
		 		
		 	});
		 	
		 	if(this.userIds&&this.userIds.length>0){
				var selStore = this.selGridPanel.getStore();
				var arrUserIds = this.userIds.split(',');
				var arrUserName = this.userName.split(',');
				for(var index=0;index<arrUserIds.length;index++){
					var newData = {userId: arrUserIds[index],fullname: arrUserName[index],title:3};
					var newRecord = new selStore.recordType(newData);
					this.selGridPanel.stopEditing();
					selStore.add(newRecord);
				}
		 	}
	 	}
	},//end of initUI function
	selGridPanelRowDbClick:function(){
		var store = this.selGridPanel.getStore();
		var rows = this.selGridPanel.getSelectionModel().getSelection();
		for(var i =0; i<rows.length; i++){
			store.remove(rows[i]);
		}
	},
	/**
	 * 用户GridPanel（中间的Grid)行点击
	 * @param {} grid
	 * @param {} rowIndex
	 * @param {} e
	 */
	gridPanelRowClick:function(grid,rowIndex,e){
		var selStore = this.selGridPanel.getStore();
		var rows = this.gridPanel.getSelectionModel().getSelection();
		for(var i= 0 ; i<rows.length ; i++){
			var userId = rows[i].data.userId;
			var fullname = rows[i].data.fullname;
			var title=rows[i].data.title;
			var isExist = false;
			//查找是否存在该记录
			for(var j=0;j<selStore.getCount();j++){
				if(selStore.getAt(j).data.userId == userId){
					isExist = true;
					break;
				}
			}
			if(!isExist){
				var newData = {userId : userId,fullname : fullname,title:title};
				var newRecord = selStore.getModel().create(newData);
				selStore.add(newRecord);
			}
		}
	},
	/**
	 * 选择用户
	 */
	confirm:function(){
		var userIds = '';
		var fullnames = '';
		//返回的用户集
		var users=[];
		if(this.single){//选择单个用户
			var rows = this.gridPanel.getSelectionModel().getSelection();
			for (var i = 0; i < rows.length; i++) {
				if (i > 0) {
					userIds += ',';
					fullnames += ',';
				}
				userIds += rows[i].data.userId;
				fullnames += rows[i].data.fullname;
				users.push(rows[i].data);
			}
		} else {
			var selStore = this.selGridPanel.getStore();
			for(var i = 0 ; i<selStore.getCount(); i++){
				if (i > 0) {
					userIds += ',';
					fullnames += ',';
				}
				userIds += selStore.getAt(i).data.userId;
				fullnames += selStore.getAt(i).data.fullname;
				users.push(selStore.getAt(i).data);
			}
		}

		if (this.callback){
			this.callback.call(this.scope, userIds, fullnames, users);
		}
		this.close();
	}
});